package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.admin.StoreSaleListAndPageDto;
import com.frejoys.common.dto.store.StoreSaleListDto;
import com.frejoys.common.enums.EFileSize;
import com.frejoys.common.util.AppConfigUtil;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.admin.StoreStatisticsListVo;
import com.frejoys.common.vo.store.StoreStatisticsYearVo;
import com.frejoys.dao.entity.Store;
import com.frejoys.dao.entity.StoreStatisticsMonth;
import com.frejoys.dao.entity.StoreStatisticsYear;
import com.frejoys.dao.mapper.StoreMapper;
import com.frejoys.dao.mapper.StoreStatisticsYearMapper;
import com.frejoys.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangzhicheng
 * @version 1.0
 * @date 2024/4/20 15:09
 */
@Service
@RequiredArgsConstructor
public class StoreStatisticsYearServiceImpl extends ServiceImpl<StoreStatisticsYearMapper, StoreStatisticsYear> implements StoreStatisticsYearService {

    private final OrderService orderService;

    private final StoreStatisticsMonthService storeStatisticsMonthService;

    private final StoreStatisticsDayService storeStatisticsDayService;

    private final StoreService storeService;

    private final StoreMapper storeMapper;


    private final StoreUserService storeUserService;


    @Override
    public List<StoreStatisticsYearVo> getList(StoreSaleListDto dto, Long userId) {

        List<Integer> storeIdByUserId = storeUserService.getStoreIdByUserId(userId);
        if (CollectionUtils.isEmpty(storeIdByUserId)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<StoreStatisticsYear> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStoreId() != null, StoreStatisticsYear::getStoreId, dto.getStoreId());
        queryWrapper.in(StoreStatisticsYear::getStoreId, storeIdByUserId);
        if (dto.getY() != null && dto.getY() != 0) {
            queryWrapper.eq(StoreStatisticsYear::getY, dto.getY());
        }
        Page<StoreStatisticsYear> page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);
        List<StoreStatisticsYear> records = baseMapper.selectPage(page, queryWrapper).getRecords();
        List<StoreStatisticsYearVo> vos = BeanUtil.copyToList(records, StoreStatisticsYearVo.class);
        List<Integer> storeIdList = vos.stream().map(StoreStatisticsYearVo::getStoreId).toList();
        if (CollectionUtils.isEmpty(storeIdList)) {
            return Collections.emptyList();
        }
        Map<Integer, String> idNameMap = storeService.getIdNameMap(storeIdList);
        for (StoreStatisticsYearVo vo : vos) {
            vo.setStoreName(idNameMap.get(vo.getStoreId()));
        }
        return vos;
    }


    @Override
    public IPage<StoreStatisticsListVo> storeStatisticsYearList(StoreSaleListAndPageDto dto) {
        Page<StoreStatisticsYear> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<StoreStatisticsYear> wrapper = new LambdaQueryWrapper<>();
        if (dto.getStoreId() != null){
            wrapper.eq(StoreStatisticsYear::getStoreId,dto.getStoreId());
        }
        if (StrUtil.isNotEmpty(dto.getStoreName())){
            List<Store> stores = storeMapper.getStoreNameById(dto.getStoreName());
            if (stores.isEmpty()){
                return new Page<>();
            }
            List<Integer> storeIds = stores.stream().map(Store::getId).toList();
            wrapper.in(StoreStatisticsYear::getStoreId,storeIds);
        }
        if (StrUtil.isNotEmpty(dto.getStartTime()) && StrUtil.isNotEmpty(dto.getEndTime())){
            Integer startTime = Integer.valueOf(TimeUtil.formatY(TimeUtil.unixTime(dto.getStartTime())));
            Integer endTime = Integer.valueOf(TimeUtil.formatY(TimeUtil.unixTime(dto.getEndTime())));

            wrapper.ge(StoreStatisticsYear::getY,startTime);
            wrapper.le(StoreStatisticsYear::getY,endTime);
        }
        wrapper.orderByDesc(StoreStatisticsYear::getCreateTime);
        Page<StoreStatisticsYear> storeStatisticsDayPage = baseMapper.selectPage(page, wrapper);
        IPage<StoreStatisticsListVo> storeStatisticsListVoIPage = storeStatisticsDayPage.convert(storeStatisticsDay -> BeanUtil.copyProperties(storeStatisticsDay, StoreStatisticsListVo.class));
        List<StoreStatisticsListVo> list = storeStatisticsListVoIPage.getRecords();

        if (!list.isEmpty()){
            List<Integer> storeIds = list.stream().map(StoreStatisticsListVo::getStoreId).distinct().toList();

            //查询店铺信息
            List<Store> stores = storeMapper.getStoreNameList(storeIds);
            Map<Integer, Store> storeMap = stores.stream().collect(Collectors.toMap(Store::getId, store -> store));

            for (StoreStatisticsListVo vo:list) {

                if (storeMap.containsKey(vo.getStoreId())){
                    vo.setStoreName(storeMap.get(vo.getStoreId()).getName());
                    vo.setLogoUrl(AppConfigUtil.getUrlPrefix(storeMap.get(vo.getStoreId()).getLogo(), EFileSize.logo.getValue()));
                }
            }
        }
        return storeStatisticsListVoIPage;
    }
}
